import numpy as np
import csv
import random
from datetime import datetime
from time import strftime
from scipy.stats import scoreatpercentile

'''
revision of test24
to calculate the false nagetive, false positive

'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate


def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    i = 0
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            print temp
            sKey.append(temp)
        if i > 10:
            break
        i += 1
            
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def build_data(inputFile):
    sKey = []
    reader = csv.reader(open(inputFile, 'rb'), delimiter=',')
    i = 0
    j = 1
    t = 2000000
    for record in reader:
        if i > 0:
            for item in record:
                sKey.append(item)
            sKey.append(generate_fips(record[1], record[2]))
            sKey.append(generate_fips(record[3], record[4]))
        else:
            numColumn = len(record)
        if i > t:
            print i, record
            break
        i += 1
    sKey = np.array(sKey)
    sKey.shape=(-1, numColumn+2)
    return sKey

def generate_fips(stateid, countyid):
    if stateid == '' or countyid == '':
        print 'error in generate_fips' 
        return null
    if len(stateid) < 2:
        temp = '0' + str(stateid)
    else:
        temp = stateid
    if len(countyid) < 2:
        temp = temp + '00' + str(countyid)
    elif len(countyid) < 3:
        temp = temp + '0' + str(countyid)
    else:
        temp = temp + str(countyid)
    #print temp
    return temp

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()
    filePath = 'C:/_DATA/migration89_08/COUNTY Migration/csv/'
    inputCSV = filePath + 'total_in.csv'
    print inputCSV
    #print np.__version__
    data = build_data(inputCSV)
    #output = []
    #for item in data:
        #data.append(generate_fips(item[1], item[2]))
        #data.append(generate_fips(item[3], item[4]))

    #output = np.array(output)
    #output.shape = (-1, 13)

    fileLoc = filePath + 'total_in_FIPS.csv'
    np.savetxt(fileLoc, data, delimiter=',', fmt = '%s')

    print "end at " + getCurTime()
    print "========================================================================"  

           
